#include "touch.h"

#define UP 1
#define DOWN 2
#define LEFT 3
#define RIGHT 4


int slide_flag = 0;

int get_inputeven(void)
{
    int fd = 0;
    int ret = 0;
    int flag = 0;
	int x_start=-1,y_start=-1;	//坐标的初值
    int x_end = -1,y_end = -1;    //坐标终点
    struct input_event ev;
    struct timeval t_end;
    int time_consumed;
    int start,end;
    
    //1.打开触摸屏 
    lcd_init();
    
    fd = open("/dev/input/event0",O_RDONLY);//只读打开
    if(-1 == fd)
    {
        perror("open fail\n");
        return -1;
    }

    // 2.读取输入事件
    while(1)
    {
        //读取一整个输入事件的数据量，存入结构体ev中
        ret = read(fd, &ev, sizeof(struct input_event));
        //确保读取到的就是一整个输入事件
        if(sizeof(ev) == ret) //确保读取到的就是一整个输入事件
        {
            //表示产生了触摸屏事件
            if(ev.type == EV_ABS)    
            {
                if(ev.code == ABS_X)
                {
                    if (-1 == x_start)        //x轴
                    {
                        x_start = ev.value;	//起点
                        gettimeofday(&(ev.time),NULL);
                        start = ev.time.tv_sec;
                    }
                    x_end = ev.value;      //终点
                    
                }
                if(ev.code == ABS_Y)		//y轴
                {
                    if (-1 == y_start)
                    {
                        y_start = ev.value;
                    }
                    y_end = ev.value;      //终点
                    
                }
                if(ev.code ==ABS_PRESSURE && ev.value == 0)
                {
                    if(x_start != -1 && y_start != -1)
                    {
                        flag = 1;
                        gettimeofday(&t_end,NULL);
                        end = t_end.tv_sec;
                        time_consumed = end - start;
                        printf("time_consumed:%d\n",time_consumed);
                        if(time_consumed >= 3)
                        {
                            slide_flag ^= 1;
                            return 0;
                        }
                    }
                }
            }
            
            if(ev.type == EV_KEY && ev.code == BTN_TOUCH)   //按键事件
            {
                if(ev.value == 1)
                {
                    //表示手指触摸触摸屏的瞬间
                    printf("press:(%d,%d)\n",x_start,y_start);
                }
                else if(ev.value == 0)
                {
                    //表示手指从触摸屏离开的瞬间
                    printf("loosen:(%d,%d)\n",x_end,y_end);
                    return 0;
                }
            }
            
            
            if(flag == 1)
            {
                if (fabs(x_end - x_start) > fabs(y_end - y_start))
                {
                    if (x_end - x_start > 0)
                    {
                        return 4;
                    }
                    else
                    {
                        return 3;
                    }
                    
                }
                
                if (fabs(x_end - x_start) < fabs(y_end - y_start))
                {
                    if (y_end - y_start > 0)
                    {
                        return 2;
                    }
                    else
                    {
                        return 1;
                    }
                    
                }
                
                flag = 0;
            }
        }
    }
    
    //关闭触摸屏
    lcd_uninit();
}


